package com.yellowpage.ca.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ca.data.RoleFunction;
import com.yellowpage.ca.service.RoleFunctionService;

public class RoleFunctionAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<RoleFunction> roleFunctionList = new ArrayList<RoleFunction>();
	private List<Map> resultList = new ArrayList<Map>();

	private RoleFunction roleFunction;
	private RoleFunctionService roleFunctionService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_roleFunctionId;
	private String query_roleId;
	private String query_functionId;
	private String query_createUser;
	private String query_createTime;

	private Map roleMap;
	private Map functionMap;

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.roleFunction = roleFunctionService.getRoleFunction(Long
					.parseLong(selectedId));
		}
		
		roleMap = roleFunctionService.getRoleMap();
		functionMap = roleFunctionService.getFunctionMap();
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("roleFunctionId", query_roleFunctionId);
		queryMap.put("roleId", query_roleId);
		queryMap.put("functionId", query_functionId);
		queryMap.put("createUser", query_createUser);
		queryMap.put("createTime", query_createTime);

		resultList = roleFunctionService.queryRoleFunctionByPage(queryMap,
				pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		roleFunction.setCreateTime(new Date());
		roleFunction.setCreateUser(loginUser.getUserId());

		roleFunctionService.saveRoleFunction(roleFunction);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {

		this.setValidatorResult("/ca2/RoleFunctionAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		roleFunction = roleFunctionService.getRoleFunction(id);

		return RESULT_EDIT;
	}

	public String update() {

		roleFunctionService.updateRoleFunction(roleFunction);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		this.setValidatorResult("/ca2/RoleFunctionUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.roleFunctionService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setRoleFunction(RoleFunction roleFunction) {
		this.roleFunction = roleFunction;
	}

	public RoleFunction getRoleFunction() {
		return roleFunction;
	}

	public void setRoleFunctionService(RoleFunctionService roleFunctionService) {
		this.roleFunctionService = roleFunctionService;
	}

	public List<RoleFunction> getRoleFunctionList() {
		return roleFunctionList;
	}

	public void setRoleFunctionList(List<RoleFunction> roleFunctionList) {
		this.roleFunctionList = roleFunctionList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_roleFunctionId() {
		return this.query_roleFunctionId;
	}

	public void setQuery_roleFunctionId(String roleFunctionId) {
		this.query_roleFunctionId = roleFunctionId;
	}

	public String getQuery_roleId() {
		return this.query_roleId;
	}

	public void setQuery_roleId(String roleId) {
		this.query_roleId = roleId;
	}

	public String getQuery_functionId() {
		return this.query_functionId;
	}

	public void setQuery_functionId(String functionId) {
		this.query_functionId = functionId;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public Map getRoleMap() {
		return roleMap;
	}

	public void setRoleMap(Map roleMap) {
		this.roleMap = roleMap;
	}

	public Map getFunctionMap() {
		return functionMap;
	}

	public void setFunctionMap(Map functionMap) {
		this.functionMap = functionMap;
	}

}
